﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Extractors;
using Winista.Text.HtmlParser.Nodes;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Visitors;
using Winista.Text.HtmlParser.Filters;

namespace seo.util
{
     public class HomeUrlGetter
    {
        public List<string> GetAllUrlFromHtml(string html)
        {
            Parser parser = Parser.CreateParser(html, "utf-8");
            NodeFilter filter = new NodeClassFilter(typeof(ATag));
            NodeList list = parser.ExtractAllNodesThatMatch(filter);

            List<string> urlList = new List<string>();
            ISimpleNodeIterator iterator = list.Elements();
            while (iterator.HasMoreNodes())
            {
                INode node = iterator.NextNode();
                ATag a = (ATag)node;
                if (a.Link.Trim().Length > 0) urlList.Add(a.Link);
            }
            return urlList;
        }

        public void GetAllUrlFromUrl(string url)
        {
            HttpHelper hh= new HttpHelper();
            string html = hh.sendRequest(url);
            GetAllUrlFromHtml(html);
        }

        public void SaveAllUrls(List<string> list)
        {
            HttpHelper hh = new HttpHelper();
            foreach (string url in list)
            {
                string html = hh.sendRequest(url);
                Parser parser = Parser.CreateParser(html, "utf-8");
                NodeFilter filter = new NodeClassFilter(typeof(MetaTag));
                NodeList metalist = parser.ExtractAllNodesThatMatch(filter);
                ISimpleNodeIterator iterator = metalist.Elements();
                MetaTag keywordsTag = null;
                while (iterator.HasMoreNodes())
                {
                    INode node = iterator.NextNode();
                    MetaTag meta = (MetaTag)node;
                    if (string.Compare(meta.MetaTagName, "keywords", true) == 0)
                    {
                        keywordsTag = meta;
                        break;
                    }
                }
                if (keywordsTag != null)
                {
                    string keyword = keywordsTag.MetaContent;
                    // save keyword and url to database
                }
            }
        }
    }
}
